package com.zhixiang.core.command;


import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zhixiang.core.util.ParamUtil;
import com.zhixiang.core.web.paging.PagingBean;

@SuppressWarnings({"rawtypes","unchecked"})
public class QueryFilter
{
    private boolean isExport;

    public static final String ORDER_DESC = "desc";

    public static final String ORDER_ASC = "asc";

    public static final Log logger = LogFactory.getLog(QueryFilter.class);

    private HttpServletRequest request;

    private String filterName;

    private List paramValues;

    private List commands;

    private Map<String, Object> aliasSet;
    
    //ptli null 代表分页  /非 null 代表不分页
    private Integer isPage;

    private PagingBean pagingBean;

    public String getFilterName()
    {
        return this.filterName;
    }

    public void setFilterName(String s)
    {
        this.filterName = s;
    }

    public PagingBean getPagingBean()
    {
        return this.pagingBean;
    }

    public void setPagingBean(PagingBean pagingBean)
    {
        this.pagingBean = pagingBean;
    }

    
    public QueryFilter()
    {
        this.isExport = false;
        this.request = null;
        this.filterName = null;
        this.paramValues = new ArrayList();
        this.commands = new ArrayList();
        this.aliasSet = new HashMap<String, Object>();
        //lpt add
        this.pagingBean = new PagingBean();
        this.isPage = 0;//默认不分页
    }

    public QueryFilter(HttpServletRequest httpservletrequest)
    {
        /*
         * Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { Boolean
         * is_true = AppUtil.getStart(); if (!is_true.booleanValue()) {
         * QueryFilter.logger.error("未检查到授权或授权已经过期....");
         * QueryFilter.logger.error("服务停止，请尽快购买授权...."); System.exit(0); } else {
         * QueryFilter.logger.info("检查授权成功...."); } } } , 30000L);
         */

        this.isExport = false;
        this.request = null;
        this.filterName = null;
        this.paramValues = new ArrayList();
        this.commands = new ArrayList();
        this.aliasSet = new HashMap<String, Object>();
        this.pagingBean = null;
        this.request = httpservletrequest;
        Enumeration enumeration = httpservletrequest.getParameterNames();

        while (enumeration.hasMoreElements())
        {
            String s = (String)enumeration.nextElement();
            if (s.startsWith("Q_"))
            {
                String s1 = httpservletrequest.getParameter(s);
                addFilter(s, s1);
            }
        }
        Integer integer = Integer.valueOf(0);
        Integer integer1 = PagingBean.DEFAULT_PAGE_SIZE;
        String s2 = httpservletrequest.getParameter("start");
        String s3 = httpservletrequest.getParameter("limit");
        //add ptli
        String isPage = httpservletrequest.getParameter("isPage");
        if (StringUtils.isNotEmpty(isPage)){
            this.isPage = 0;
        } 
        if (StringUtils.isNotEmpty(s2)) integer = new Integer(s2);
        if (StringUtils.isNotEmpty(s3)) integer1 = new Integer(s3);
        String s4 = httpservletrequest.getParameter("sort");
        String s5 = httpservletrequest.getParameter("dir");
        if ((StringUtils.isNotEmpty(s4)) && (StringUtils.isNotEmpty(s5))) addSorted(s4, s5);
        if ("true".equals(httpservletrequest.getParameter("isExport")))
        {
            this.isExport = true;
            httpservletrequest.setAttribute("colId", httpservletrequest.getParameter("colId"));
            httpservletrequest.setAttribute("colName", httpservletrequest.getParameter("colName"));
            httpservletrequest.setAttribute("exportType",
                httpservletrequest.getParameter("exportType"));
        }
        httpservletrequest.setAttribute("isExport", Boolean.valueOf(this.isExport));
        this.pagingBean = new PagingBean(integer.intValue(), integer1.intValue());
    }

   
    public void addFilter(String s, String s1)
    {
        String[] as = s.split("[_]");
        Object obj = null;
        if ((as != null) && (as.length == 4))
        {
            obj = ParamUtil.convertObject(as[2], s1);
            if (obj != null)
            {
                FieldCommandImpl fieldcommandimpl = new FieldCommandImpl(as[1], obj, as[3], this);
                this.commands.add(fieldcommandimpl);
            }
        }
        else if ((as != null) && (as.length == 3))
        {
            FieldCommandImpl fieldcommandimpl1 = new FieldCommandImpl(as[1], obj, as[2], this);
            this.commands.add(fieldcommandimpl1);
        }
        else
        {
            logger.error("Query param name [" + s + "] is not right format.");
        }
    }

    public void addParamValue(Object obj)
    {
        this.paramValues.add(obj);
    }

    public List getParamValueList()
    {
        return this.paramValues;
    }

    public void addSorted(String s, String s1)
    {
        this.commands.add(new SortCommandImpl(s, s1, this));
    }

    public void addExample(Object obj)
    {
        this.commands.add(new ExampleCommandImpl(obj));
    }

    public List getCommands()
    {
        return this.commands;
    }

    public Map<String, Object> getAliasSet()
    {
        return this.aliasSet;
    }

    public boolean isExport()
    {
        return this.isExport;
    }

    public void setExport(boolean flag)
    {
        this.isExport = flag;
    }

    public HttpServletRequest getRequest()
    {
        return this.request;
    }

    public Integer getIsPage()
    {
        return isPage;
    }

    public void setIsPage(Integer isPage)
    {
        this.isPage = isPage;
    }

    
}
